<template>
	<view class="flex-column">
		<view class="baseView" style="padding: 40rpx;">

			<view class="mainColor shadowView"
				style="margin-top: 20rpx; margin-bottom: 15rpx; font-size: 18px; font-weight: 500;">
				{{model.title ?? '--'}}
			</view>

			<view class="shadowView flex-column">
				<view class="blockTextColor" style="font-size: 16px;">
					资源简介：
				</view>
				<!--  -->
				<view class="subTextColor subText" style="margin-top: 20rpx; font-size: 16px;">
					{{model.description ?? '暂无简介'}}
				</view>
			</view>
			
			<!--  -->
			<view v-if="openAd==true">
				<!-- 资源链接 -->
				<view v-if="isSee==false"class="shadowView flex-column">
					<view class="blockTextColor" style="font-size: 16px;">
						资源链接：
					</view>
					<!--  -->
					<view class="subTextColor subText" style="margin-top: 20rpx; font-size: 18px; ">
						{{getLink()}}
					</view>
					<view class="row-lineConter tipsTextColor" style="margin-top: 5px;">
						<uni-icons type="info" size="18" color="rgb(224, 168, 99)"></uni-icons>
						<view style="margin-left: 10rpx;">
							点击获取资源，观看后可获得资源
						</view>
					</view>
				</view>
				
				<!-- 资源链接 -->
				<view v-if="isSee==true" class="shadowView flex-column">
					<view class="blockTextColor" style="font-size: 16px;">
						资源链接：
					</view>
					<!--  -->
					<view class=" subText mainColor" style="margin-top: 20rpx; font-size: 18px; ">
						{{model.originalLinkContent}}
					</view>
				
					<view class="zsConter" style="margin-top: 20rpx">
						<view class="bottomBtn zsConter mainBgColor" style="margin-bottom: 0px;" @click="copyMethod()">
							复制链接
						</view>
					</view>
					
					<view v-if="isBaiduLink()==true" class="zsConter" style="margin-top: 16rpx">
						<view class="bottomBtn zsConter mainBgColor" style="margin-bottom: 20px;" @click="openurl(model.extractLinks)">
							点击跳转百度网盘小程序提取
						</view>
					</view>
				</view>
			</view>
			<view  v-if="openAd==false">
				<!-- 资源链接 -->
				<view class="shadowView flex-column">
					<view class="blockTextColor" style="font-size: 16px;">
						资源链接：
					</view>
					<!--  -->
					<view class=" subText mainColor" style="margin-top: 20rpx; font-size: 18px; ">
						{{model.originalLinkContent}}
					</view>
				
					<view class="zsConter" style="margin-top: 20rpx">
						<view class="bottomBtn zsConter mainBgColor" style="margin-bottom: 0px;" @click="copyMethod()">
							复制链接
						</view>
					</view>
					
					<view v-if="isBaiduLink()==true" class="zsConter" style="margin-top: 16rpx">
						<view class="bottomBtn zsConter mainBgColor" style="margin-bottom: 20px;" @click="openurl(model.extractLinks)">
							点击跳转百度网盘小程序提取
						</view>
					</view>
				</view>
			</view>


			<!-- 图片 -->
			<view  class="shadowView flex-column">
				<view class="blockTextColor" style="margin-top: 10rpx; font-size: 16px;">
					资源预览图：
				</view>
				<!--  -->
				<view v-for="(imageUrl,index) in model.resourceImages" :key="index">
					<image @click='previewImage(imageUrl)' :src="imageUrl" style="width: 100%; margin-top: 10px;"
						mode="widthFix"></image>
				</view>
			</view>
			<!-- 分享按钮 -->
			<view style="margin: 30rpx 0rpx;" class="">
				<BtnsView @btnsClick='btnsClick'></BtnsView>
			</view>


			<!-- //弹窗 -->
			<uni-popup ref="popup" type="center">
				<shareMomentsView v-if="showSharePopup==true" @closeClick='this.$refs.popup.close()'
					:menuButtonInfo="menuButtonInfo"></shareMomentsView>
			</uni-popup>
		</view>

		<expressAdView v-if="openAd == true"></expressAdView>
		
		<view style="height: 80rpx;">
			
		</view>


		<view v-if="getShowBottomBtn()==true">
			<view v-if="isSee == false" class="bottomBar zsConter">
				<view class="bottomBtn zsConter mainBgColor" style="margin-bottom: 20px;" @click="seeBtnClick()">
					获取资源
				</view>
			</view>
		</view>
		<view v-if="scene == 1154">
			<PushTipView ></PushTipView>
		</view>
		

	</view>
</template>

<script>
	import BtnsView from '../../components/btns-view/btns-view.vue'
	import shareMomentsView from '../../components/share-moments-view/share-moments-view.vue'
	import expressAdView from '../../components/express-ad-view/express-ad-view.vue'
	import PushTipView from '../../components/push-tip-view/push-tip-view.vue'

	export default {
		components: {
			BtnsView,
			shareMomentsView,
			expressAdView,
			PushTipView
		},
		data() {
			return {
				seeAuthorQRcode: false, //是否查看作者
				shareImageUrl: 'https://mp-35bd1c1b-8095-44b6-b494-c65e6d85fd9c.cdn.bspapp.com/appConfigure/资源分享图.jpg',
				model:{},
				showSharePopup: false,
				menuButtonInfo: {},
				openAd:false,
				videoAdId:'',
				isSee:false,// 是否可以查看资源
				scene:0,//场景值  1154 还在朋友圈中查看没有进入  1155 点击前往小程序按钮进入
			}
		},
		onReady() {
			// 在页面准备好时获取胶囊按钮的位置信息
			this.menuButtonInfo = uni.getMenuButtonBoundingClientRect();
			console.log('胶囊信息===', this.menuButtonInfo);
		},
		onLoad(options) {
			let that = this;
			uni.$on('RefreshView', (data) => {
				console.log('收到通知刷新:', data);
				that.scene = data.scene
				that.$forceUpdate()
			});
			
			if (options.model == null) { //分享进入
			   let openAd = options.openAd
			   let videoAdId = options.videoAdId
			   console.log('options==',options)
				this.$uniCloudApi.get_resources_details(options.dataId).then(res=>{
					that.model = res;
					that.openAd = openAd == '1' ? true:false;
					that.videoAdId = videoAdId
					if(that.openAd == true){
						that.testIsSee();
					}
					that.loadAd()
					uni.setNavigationBarTitle({
						title: that.model.title
					})
					
				}).catch(res=>{
					// that.$showToast(res);
				})

			} else {
				this.videoAdId = this.$videoAdId
				this.model = this.$decodeJson(options.model);
				uni.setNavigationBarTitle({
					title: this.model.title
				})
				console.log('this.openAd22==',this.openAd);
				this.openAd = this.$openAd == '1' ? true:false;
				if(this.openAd == true){
					this.testIsSee();
				}
				this.loadAd()
			}
			
		},
		onUnload() {
			// 移除监听事件，避免重复监听
			uni.$off('RefreshView');
		},
	

		       
		// 组件的methods部分
		methods: {
			getShowBottomBtn(){
				if(this.openAd == false){
					return false
				}
			  if(this.scene == 0 || this.scene == 1155){
				  return true
			  }	
			  if(this.scene == 1154){
				  return false;
			  }
			  return true;
			},
			testIsSee(){
				if(this.$idList.includes(this.model._id)){
					this.isSee = true;
				} else {
					this.isSee = false;
				}
			},
			copyMethod() {
				if(this.scene == 1154){
					uni.showToast({
						title: '点击右下角“前往小程序”按钮后，再次点击复制即可',
						icon: 'none'
					});
					return
				}
				if(this.$idList.includes(this.model._id) == false){
					this.addIdList(this.model._id);
				}
				uni.setClipboardData({
					data: this.model.originalLinkContent,
					success: function() {
						uni.showToast({
							title: '链接已复制到剪切板',
							icon: 'success'
						});
					},
					fail: function(error) {
						uni.showToast({
							title: '复制失败',
							icon: 'none'
						});
						console.log('失败', error);
					}
				});
			},
			//看视频
			showAd() {
				if (this.ad) {
					this.ad.show().then(() => {
						console.log('激励视频广告显示成功');
					}).catch((err) => {
						console.error('激励视频广告显示失败', err);
					});
				}
			},
			loadAd() {
				if (wx.createRewardedVideoAd) {
					this.ad = wx.createRewardedVideoAd({
						adUnitId: this.videoAdId
					});
					
					this.ad.onLoad(() => {
						console.log('激励视频广告加载成功');
					});
					this.ad.onError((err) => {
						console.error('激励视频广告加载失败', err);
					});
					this.ad.onClose((res) => {
						console.log('onClose event emit11', res);
						if (res && res.isEnded) {
							console.log('用户完整观看了广告，可以发放奖励');
							this.isSee = true;
							this.copyMethod()
						    
						} else {
							uni.showModal({
								title: '温馨提示', // 弹窗的标题
								content: '观看完整广告才可获取资源', // 弹窗的内容
								showCancel: false, // 是否显示取消按钮，默认为true
								confirmText: '确定', // 确定按钮的文字，默认为"确定"
							});
						}
					});
				} else {
					console.log('开始加载广告==没有加载')
				}

			},
			getLink() {
				if(this.model.originalLinkContent == undefined){
					return ''
				}
				var link = this.model.originalLinkContent;
				let position = link.indexOf('https://');
				var str = link.substring(position, 25);
				let replacedString = link.replace(str, "*****")
				// console.log('字符所在位置==',position,'截取的字符串=',str,'结果==',replacedString);
				return replacedString;
			},
			addIdList(dataId){
				var idList = []
				if (this.$idList) {
					idList = this.$idList
				}
				idList.push(dataId);
				this.$setIdList(idList)
				uni.setStorage({
					key: 'idList',
					data: idList,
					success(lres) {
						console.log('存本地成功=', lres)
					},
					fail(failres) {
						console.log('存本地失败=', failres)
					}
				})
				console.log('idList==', this.$idList)
			},
			seeBtnClick() {
				var that = this;
				uni.showModal({
					title: '温馨提示', // 弹窗的标题
					content: '观看完视频广告即可获得资源', // 弹窗的内容
					showCancel: true, // 是否显示取消按钮，默认为true
					confirmText: '观看获得', // 确定按钮的文字，默认为"确定"
					cancelText: '取消',
					success: function(res) {
						if (res.confirm) {
							that.showAd();
							console.log('用户点击确定');
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			btnsClick(data) {
				if (data.index == 1) {
					this.showSharePopup = true;
					this.$refs.popup.open('center')
				} else {
					uni.navigateTo({
						url: '../integral-core-page/integral-core-page'
					})
				}
			},
			previewImage(imageUrl) {
				// 预览图片
				uni.previewImage({
					urls: this.model.resourceImages, // 需要预览的图片链接列表
					current: imageUrl // 为当前显示图片的链接/索引值
				});
			},
			toResultPage() {
				var model = this.model;
				uni.navigateTo({
					url: '../result-page/result-page?model=' + this.$encodeJson(model)
				});
			},
			adOnLoad() {
				console.log('广告加载成功');
				uni.showToast({
					title: '广告加载成功'
				})
			},

			getShareStr(senderFriend) {
				var path = 'pages/resource-details-page/resource-details-page?'
				var dataId = this.model._id;
				var openAd = this.$openAd;
				var friendPush = senderFriend == true? '0':'1'
				var videoAdId = this.$videoAdId;
				var str =`sharePush=1&dataId=${dataId}&openAd=${openAd}&friendPush=${friendPush}&videoAdId=${videoAdId}`
				if (senderFriend) {
					str =`${path}&sharePush=1&dataId=${dataId}&openAd=${openAd}&friendPush=${friendPush}&videoAdId=${videoAdId}`
				}
				console.log('getShareStr==', str)
				return str;
			},
			getShareImageUrl() {
				if (this.model.resourceImages.length > 0) {
					return this.model.resourceImages[0]
				}
				return this.shareImageUrl;
			},
			isBaiduLink() {
				if(this.model.originalLinkContent == null){
					return false;
				}
				if (this.model.originalLinkContent.includes('baidu.com')) {
					return true;
				}
				return false;
			},
			getStrUrl(s) {
				var reg = /(http:\/\/|https:\/\/)((\w|=|\?|\.|\/|&|-)+)/g;
				var reg = /(https?|http|ftp|file):\/\/[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]/g;
				s = s.match(reg);
				return (s && s.length ? s[0] : null);
			},
			openurl(url) {
				let cjurl = this.getStrUrl(url) //净化过后的url
				//小程序跳转
				let tzurl = cjurl.split('/s/')[1].split('?pwd')[0] //取出来链接
				let pwd = cjurl.split('/s/')[1].split('?pwd=')[1] //取出来提取码
				console.log('url==', tzurl, 'pwd=', pwd);
				uni.navigateToMiniProgram({
					appId: 'wxdcd3d073e47d1742',
					path: `pages/netdisk_share/share?scene=${tzurl}%23${pwd}%238%230`,
					extraData: {
						foo: 'bar'
					},
					envVersion: 'release',
					success(res) {
						// 打开成功
					}
				})
			},
		},
		onShareAppMessage(res) {
			var title = '免费资源【'+this.model.title+'】网盘链接点击提取'
			let data = {
				title: title, //转发标题
				path: this.getShareStr(true),
				imageUrl: this.getShareImageUrl(), //转发图片
				success: function(res) {
					console.log('分享成功', res);
					uni.showToast({
						title: '分享成功'
					})
					// 分享成功的处理逻辑
				},
			}
			return data
		},
		onShareTimeline() {
			var title ='免费资源【'+this.model.title+'】网盘链接点击提取'
			return {
				title: title,
				query: this.getShareStr(false),
				imageUrl: this.getShareImageUrl(),
				success: function(res) {
					console.log('分享成功', res);
					this.showSharePopup = false;
					this.$refs.popup.close()
					uni.showToast({
						title: '分享成功'
					})
					// 分享成功的处理逻辑
				},
				fail: function(err) {
					console.error('分享失败', err);
					// 分享失败的处理逻辑
				},
				complete: function(res) {
					console.log('分享结束', res);
					// 分享结束的处理逻辑
				} //转发图片
			}
		}
	}
</script>

<style lang="scss" scoped>
	.tipsView {
		color: rgb(224, 168, 99);
		font-size: 30rpx;
		border-radius: 8rpx;
		padding: 16rpx;
		background-color: rgba(224, 168, 99, 0.2);
		border: 1px solid rgb(224, 168, 99);
	}

	.baseView {
		// background-image: url('https://mp-35bd1c1b-8095-44b6-b494-c65e6d85fd9c.cdn.bspapp.com/appConfigure/topBgImage.png');
		background-repeat: no-repeat;
		/* 背景图片不重复 */
		background-position: center;
		/* 背景图片居中显示 */
		background-size: cover;
		/* 背景图片覆盖整个元素区域 */
	}

	.shadowView {
		padding: 30rpx;
		overflow: hidden;
		// border-radius: 8rpx;
		// box-shadow: 0rpx 0rpx 8rpx gainsboro;
	}

	.imageView {
		width: 110rpx;
		height: 110rpx;
		border-radius: 8rpx;
	}

	.bottomBar {
		background-color: white;
		position: fixed;
		left: 0rpx;
		bottom: 0rpx;
		width: 100%;
		height: 130rpx;
	}

	.bottomBtn {
		color: white;
		font-size: 30rpx;
		font-weight: bold;
		height: 80rpx;
		border-radius: 40rpx;
		width: 80%;
	}

	.subText {
		font-size: 15px;

	}
</style>